<?php

// Namespace

namespace Loop;


require_once(PATH_LOOP . 'Model/CNotification.php');
require_once(PATH_LOOP . 'Class/CNotificationManager.php');


/**
 * @package		Loop
 * @category	Class
 */
 
class CNotification_Test extends \UnitTestCase
{
	/*
	*
	*/

	public function testNotifications()
	{
		// Create objects

		$notification = new \Loop\CNotification();
		$notificationManager = new \Loop\CNotificationManager();


		// Create the asynchronous notification

		$notification->setId(1);
		$notification->setExtensionId('Engage-Reward');
		$notification->setNotificationName('Operation.NotifyRecipients');
		$notification->setNotificationTarget('cbonachera@splio.fr');
		$notification->setNotificationType('email');
		$notification->setNotificationVariables('{"operationName":"Pecan Pals","operationDescription":null,"username":"Ptikado Theys","points":{"nbNQPoints":"50","nbQPoints":"5"}}');
		$notification->setIsUnique(1);

		$notification->insert();


		$notificationManager->api_sendNotifications();


		$rawNotifications = $notification->find();


		$notification->load(1, null, array('acceptDeletedObjects' => true));


		// Test

		$this->assertNotNull($notification->getTimeOfDeletion());
		$this->assertEqual(count($rawNotifications), 1);


		// Extract the raw

		$rawNotification = array_pop($rawNotifications);


		// Load the object

		$notification->loadFromRawObject($rawNotification);


		// Tests

		$this->assertEqual($notification->getId(), '2');
		$this->assertEqual($notification->getExtensionId(), 'Engage-Reward');
		$this->assertEqual($notification->getKey(), '085bda488007746c3b5a3cc38b87ba09');
		$this->assertEqual($notification->getNotificationName(), 'Operation.NotifyRecipients');
		$this->assertEqual($notification->getNotificationTarget(), 'cbonachera@splio.fr');
		$this->assertEqual($notification->getNotificationType(), 'email');
		$this->assertNotNull($notification->getTimeOfExpedition());


		/**********************************************************************************************/

		// Create 2 same notification in unique mode


		$notification->reset();

		$notification->setId(3);
		$notification->setExtensionId('Engage-Reward');
		$notification->setNotificationName('Operation.NotifyRecipients');
		$notification->setNotificationTarget('cbonachera@splio.fr');
		$notification->setNotificationType('email');
		$notification->setNotificationVariables('{"operationName":"Pecan Pals","operationDescription":null,"username":"Ptikado Theys","points":{"nbNQPoints":"50","nbQPoints":"5"}}');
		$notification->setIsUnique(1);

		$notification->insert();


		// Call the api

		$notificationManager->api_sendNotifications();


		// Find the notification

		$rawNotifications = $notification->find();


		// Test 

		$this->assertEqual(count($rawNotifications), 2);



		// For the first notification Found

		$notification->load(2);	


		// Test

		$this->assertEqual($notification->getId(), $rawNotifications[2]['id']);



		// For the second notification found

		$notification->load(3);


		// Test

		$this->assertEqual($notification->getId(), $rawNotifications[3]['id']);

		$this->assertNull($notification->getTimeOfDeletion());
		$this->assertNull($notification->getTimeOfExpedition());	


		// Delete the notification

		$notification->delete()	;


		/**********************************************************************************************/

		// Create 2 same notification without unique mode


		$notification->reset();

		$notification->setId(4);
		$notification->setExtensionId('Engage-Reward');
		$notification->setNotificationName('Operation.NotifyRecipients');
		$notification->setNotificationTarget('cbonachera@splio.fr');
		$notification->setNotificationType('email');
		$notification->setNotificationVariables('{"operationName":"Pecan Pals","operationDescription":null,"username":"Ptikado Theys","points":{"nbNQPoints":"50","nbQPoints":"5"}}');
		$notification->setIsUnique(0);

		$notification->insert();


		// Load the notification 2 and change the mode

		$notification->load(2);
		$notification->setIsUnique(0);
		$notification->save();


		// Call the api

		$notificationManager->api_sendNotifications();


		// Find the notification
		
		$rawNotifications = $notification->find();


		// Test 

		$this->assertEqual(count($rawNotifications), 2);



		// For the first notification Found

		$notification->load(2);	


		// Test

		$this->assertEqual($notification->getId(), $rawNotifications[2]['id']);



		// For the second notification found

		$notification->load(5);	


		// Test

		$this->assertEqual($notification->getId(), $rawNotifications[5]['id']);
		$this->assertNotNull($notification->getTimeOfExpedition());	


		// Load the asynchronous notification

		$notification->load(4, null, array('acceptDeletedObjects' => true));


		// Test

		$this->assertNotNull($notification->getTimeOfDeletion());
	
	}
}
?>